package cn.wolf.tools;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;

import android.content.Context;

public final class FileUtils {

	/**
	 * Serialize the object
	 */
	public static void write(Context context, String fileName, Object obj) {
		ObjectOutputStream out = null;
		try {
			out = new ObjectOutputStream(context.openFileOutput(fileName, Context.MODE_PRIVATE));
			out.writeObject(obj);
			out.flush();
		} catch (FileNotFoundException e) {
			Log.e("FileNotFoundException error, message:" + e.getMessage(), e);
			// e.printStackTrace();
		} catch (IOException e) {
			Log.e("IOException error, message:" + e.getMessage(), e);
			// e.printStackTrace();
		} finally {
			StreamUtil.closeSilently(out);
		}
	}

	/**
	 * Deserialize the Object
	 */
	public static Object read(Context context, String fileName) {
		ObjectInputStream input = null;
		try {
			input = new ObjectInputStream(context.openFileInput(fileName));
			return input.readObject();
		} catch (StreamCorruptedException e) {
			Log.e(" error message:" + e.getMessage(), e);
			// e.printStackTrace();
		} catch (FileNotFoundException e) {
			Log.e(" error message:" + e.getMessage(), e);
			// e.printStackTrace();
		} catch (IOException e) {
			Log.e(" error message:" + e.getMessage(), e);
			// e.printStackTrace();
		} catch (ClassNotFoundException e) {
			Log.e(" error message:" + e.getMessage(), e);
			// e.printStackTrace();
		} finally {
			StreamUtil.closeSilently(input);
		}

		return null;
	}

	/**
	 * delete cache file
	 */
	public static boolean deleteFile(Context context, String fileName) {
		return context.deleteFile(fileName);
	}

	
	
	
	
	
	
}
